package com.intelligent.health.setmeal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.health.setmeal.dao.CheckItemMapper;
import com.intelligent.health.setmeal.dto.CheckItemDto;
import com.intelligent.health.setmeal.service.CheckItemService;
import com.intelligent.health.setmeal.pojo.CheckItem;
import com.intelligent.health.setmeal.vo.CheckItemVo;
import entity.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/03/13/22:37
 * @Description:
 */
@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public PageResult<CheckItemVo> queryCheckItemByPage(CheckItemDto checkItemDto, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper queryWrapper = getQueryWrapper(checkItemDto);
        IPage<CheckItem> checkItems = checkItemMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        PageResult<CheckItemVo> pageResult = new PageResult<>();
        ArrayList<CheckItemVo> checkItemVos = new ArrayList<>();
        checkItems.getRecords().forEach(checkItem -> {
            CheckItemVo checkItemVo = new CheckItemVo();
            BeanUtils.copyProperties(checkItem, checkItemVo);
            checkItemVos.add(checkItemVo);
        });
        pageResult.setCurrentPage(checkItems.getCurrent());
        pageResult.setTotal(checkItems.getTotal());
        pageResult.setPageSize(checkItems.getSize());
        pageResult.setRows(checkItemVos);

        return pageResult;
    }

    @Override
    public CheckItem queryCheckItemById(Integer checkItemId) {
        return checkItemMapper.selectById(checkItemId);
    }

    @Override
    public void addCheckItem(CheckItem checkItem) {
        checkItemMapper.insert(checkItem);
    }

    @Override
    public void editCheckItem(CheckItem checkItem) {
        checkItemMapper.updateById(checkItem);
    }

    @Override
    public void delCheckItemById(Integer checkItemId) {
        checkItemMapper.delCheckItemById(checkItemId);
    }

    @Override
    public void delBatchCheckItem(ArrayList<Integer> checkItemIds) {
        checkItemMapper.delBatchCheckItem(checkItemIds);
    }

    @Override
    public List<CheckItem> queryCheckItems(HashMap<String, Object> hashMap) {
        return checkItemMapper.queryCheckItemsForExcel(hashMap);
    }

    /**
     * LambdaQueryWrapper checkitem查询
     *
     * @param checkItemDto
     * @return
     */
    public static LambdaQueryWrapper getQueryWrapper(CheckItemDto checkItemDto) {

        LambdaQueryWrapper<CheckItem> queryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(checkItemDto.getName())) {
            queryWrapper.like(CheckItem::getName, checkItemDto.getName());
        }
        if (StringUtils.isNotBlank(checkItemDto.getCode())) {
            queryWrapper.like(CheckItem::getCode, checkItemDto.getCode());
        }
        if (StringUtils.isNotBlank(checkItemDto.getSex())) {
            queryWrapper.eq(CheckItem::getSex, checkItemDto.getSex());
        }
        if (StringUtils.isNotBlank(checkItemDto.getAge())) {
            queryWrapper.eq(CheckItem::getAge, checkItemDto.getAge());
        }
        if (StringUtils.isNotBlank(checkItemDto.getType())) {
            queryWrapper.eq(CheckItem::getType, checkItemDto.getType());
        }
        //前端默认 有效 1
        if (checkItemDto.getState() != null) {
            queryWrapper.eq(CheckItem::getState, checkItemDto.getState());
        }
        return queryWrapper;
    }
}
